//
//  UIView+wdp_layout.m
//  WDPubFramework
//
//  Created by gadmyth on 3/20/17.
//  Copyright © 2017 wanda. All rights reserved.
//

#import "UIView+wdp_layout.h"

// 设置View的width, height, left, top值的c方法的宏实现
#define VImplement(type) \
void V##type##set(UIView *V, CGFloat value) {\
CGRect frame = VF(V);\
F##type(frame) = value;\
VF(V) = frame;\
}

VImplement(W);
VImplement(H);
VImplement(L);
VImplement(T);

// 获取 view frame 的四个角的Point的方法macro, 比如 VTL 表示获取 top-left 的坐标
#define PImplement(y, x) \
CGPoint V##y##x(UIView *V) {\
return CGPointMake(V##x(V), V##y(V));\
}

PImplement(T, L);
PImplement(T, R);
PImplement(B, L);
PImplement(B, R);

// 获取 view bounds 的四个角的Point的方法macro
#define P2Implement(y, x) \
CGPoint V2##y##x(UIView *V) {\
return CGPointMake(V2##x(V), V2##y(V));\
}

P2Implement(T, L);
P2Implement(T, R);
P2Implement(B, L);
P2Implement(B, R);

@implementation UIView (wdp_layout)
- (void)alignTo:(UIView *)view type:(WDAlign)align margin:(CGFloat)margin {
    if (self.superview == view.superview) {
        [self alignToSibling:view type:align margin:margin];
    } else if (self.superview == view) {
        [self alignToParent:align margin:margin];
    }
}

- (void)alignToSibling:(UIView *)view type:(WDAlign)align margin:(CGFloat)margin {
    switch (align) {
        case WDAlignLeft: {
            VLset(self, VL(view) + margin);
            break;
        }
        case WDFixedAlignLeft: {
            CGFloat oldValue = VL(self);
            CGFloat newValue = VL(view) + margin;
            CGFloat delta = newValue - oldValue;
            VLset(self, newValue);
            VWset(self, VW(self) - delta);
            break;
        }
        case WDAlignRight: {
            VLset(self, VR(view) - VW(self) - margin);
            break;
        }
        case WDFixedAlignRight: {
            VWset(self, VW(self) + VR(view) - VR(self));
            break;
        }
        case WDAlignTop: {
            VTset(self, VT(view) + margin);
            break;
        }
        case WDFixedAlignTop: {
            CGFloat oldValue = VT(self);
            CGFloat newValue = VT(view) + margin;
            CGFloat delta = newValue - oldValue;
            VTset(self, newValue);
            VHset(self, VH(self) - delta);
            break;
        }
        case WDAlignBottom: {
            VTset(self, VB(view) - VH(self) + margin);
            break;
        }
        case WDFixedAlignBottom: {
            VHset(self, VH(self) + VB(view) - VB(self));
            break;
        }
        case WDAlignCenterVertical: {
            VTset(self, VT(view) + (VH(view) - VH(self)) * 0.5 + margin);
            break;
        }
        case WDAlignCenterHorizontal: {
            VLset(self, VL(view) + (VW(view) - VW(self)) * 0.5 + margin);
            break;
        }
        default: {
            break;
        }
    }
}

- (void)alignToParent:(WDAlign)align margin:(CGFloat)margin {
    
    UIView *view = self.superview;
    
    switch (align) {
        case WDAlignLeft: {
            VLset(self, 0 + margin);
            break;
        }
        case WDFixedAlignLeft: {
            CGFloat oldValue = VL(self);
            CGFloat newValue = 0 + margin;
            CGFloat delta = newValue - oldValue;
            VLset(self, newValue);
            VWset(self, VW(self) - delta);
            break;
        }
        case WDAlignRight: {
            VLset(self, VW(view) - VW(self) - margin);
            break;
        }
        case WDFixedAlignRight: {
            VWset(self, VW(view) - margin - VL(self));
            break;
        }
        case WDAlignTop: {
            VTset(self, 0 + margin);
            break;
        }
        case WDFixedAlignTop: {
            CGFloat oldValue = VT(self);
            CGFloat newValue = 0 + margin;
            CGFloat delta = newValue - oldValue;
            VTset(self, newValue);
            VHset(self, VH(self) - delta);
            break;
        }
        case WDAlignBottom: {
            VTset(self, VH(view) - VH(self) + margin);
            break;
        }
        case WDFixedAlignBottom: {
            VHset(self, VB(view) + margin - VT(self));
            break;
        }
        case WDAlignCenterVertical: {
            VTset(self, (VH(view) - VH(self)) * 0.5 + margin);
            break;
        }
        case WDAlignCenterHorizontal: {
            VLset(self, (VW(view) - VW(self)) * 0.5 + margin);
            break;
        }
        default: {
            break;
        }
    }
}


#pragma doc - 位置处于view的右边, 非实时绑定
- (void)leftTo:(UIView *)view margin:(CGFloat)margin {
    if (self.superview == view.superview) {
        [self leftToSibling:view margin:margin];
    }
    else if (self.superview == view) {
        [self leftToParent:view margin:margin];
    }
}

- (void)leftToSibling:(UIView *)view margin:(CGFloat)margin {
    CGFloat left = VL(view) - VW(self) - margin;
    VLset(self, left);
}

- (void)leftToParent:(UIView *)view margin:(CGFloat)margin {
    CGFloat left = 0 - VW(self) - margin;
    VLset(self, left);
}

#pragma doc - 位置处于view的右边，left不变，只改变right
- (void)fixedLeftTo:(UIView *)view margin:(CGFloat)margin {
    if (self.superview == view.superview) {
        [self fixedLeftToSibling:view margin:margin];
    }
    else if (self.superview == view) {
        [self fixedLeftToParent:view margin:margin];
    }
}

- (void)fixedLeftToSibling:(UIView *)view margin:(CGFloat)margin {
    VWset(self, VL(view) - VL(self) + margin);
}

- (void)fixedLeftToParent:(UIView *)view margin:(CGFloat)margin {
    VWset(self, 0 - VL(self) + margin);
}

#pragma doc - 位置处于view的右边, 非实时绑定
- (void)rightTo:(UIView *)view margin:(CGFloat)margin {
    if (self.superview == view.superview) {
        [self rightToSibling:view margin:margin];
    }
    else if (self.superview == view) {
        [self rightToParent:view margin:margin];
    }
}

- (void)rightToSibling:(UIView *)view margin:(CGFloat)margin {
    CGFloat left = VR(view) + margin;
    VLset(self, left);
}

- (void)rightToParent:(UIView *)view margin:(CGFloat)margin {
    CGFloat left = VW(view) + margin;
    VLset(self, left);
}

#pragma doc - 位置处于view的右边, right不变，只改变left
- (void)fixedRightTo:(UIView *)view margin:(CGFloat)margin {
    if (self.superview == view.superview) {
        [self fixedRightToSibling:view margin:margin];
    }
    else if (self.superview == view) {
        [self fixedRightToParent:view margin:margin];
    }
}

- (void)fixedRightToSibling:(UIView *)view margin:(CGFloat)margin {
    CGFloat oldRight = VR(self);
    CGFloat left = VR(view) + margin;
    VLset(self, left);
    VWset(self, oldRight - left);
}

- (void)fixedRightToParent:(UIView *)view margin:(CGFloat)margin {
    CGFloat oldRight = VR(self);
    CGFloat left = VW(view) + margin;
    VLset(self, left);
    VWset(self, oldRight - left);
}


#pragma doc - 位置处于view的下边, 非实时绑定
- (void)bottomTo:(UIView *)view margin:(CGFloat)margin {
    if (self.superview == view.superview) {
        [self bottomToSibling:view margin:margin];
    }
    else if (self.superview == view) {
        [self bottomToParent:view margin:margin];
    }
}

- (void)bottomToSibling:(UIView *)view margin:(CGFloat)margin {
    CGFloat top = VB(view) + margin;
    VTset(self, top);
}

- (void)bottomToParent:(UIView *)view margin:(CGFloat)margin {
    CGFloat top = VH(view) + margin;
    VTset(self, top);
}

#pragma doc - 位置处于view的下边, bottom不变，只改变top
- (void)fixedBottomTo:(UIView *)view margin:(CGFloat)margin {
    if (self.superview == view.superview) {
        [self fixedBottomToSibling:view margin:margin];
    }
    else if (self.superview == view) {
        [self fixedBottomToParent:view margin:margin];
    }
}

- (void)fixedBottomToSibling:(UIView *)view margin:(CGFloat)margin {
    CGFloat oldBottom = VB(self);
    CGFloat top = VB(view) + margin;
    VTset(self, top);
    VHset(self, oldBottom - top);
}

- (void)fixedBottomToParent:(UIView *)view margin:(CGFloat)margin {
    CGFloat oldBottom = VB(self);
    CGFloat top = VH(view) + margin;
    VTset(self, oldBottom - top);
}

#pragma doc - 位置处于view的上边, 非实时绑定
- (void)topTo:(UIView *)view margin:(CGFloat)margin {
    if (self.superview == view.superview) {
        [self topToSibling:view margin:margin];
    }
    else if (self.superview == view) {
        [self topToParent:view margin:margin];
    }
}

- (void)topToSibling:(UIView *)view margin:(CGFloat)margin {
    CGFloat top = VT(view) - VH(self) - margin;
    VTset(self, top);
}

- (void)topToParent:(UIView *)view margin:(CGFloat)margin {
    CGFloat top = 0 - VH(self) - margin;
    VTset(self, top);
}

#pragma doc - 位置处于view的上边, 不改变top, 只修改bottom
- (void)fixedTopTo:(UIView *)view margin:(CGFloat)margin {
    if (self.superview == view.superview) {
        [self fixedTopToSibling:view margin:margin];
    }
    else if (self.superview == view) {
        [self fixedTopToParent:view margin:margin];
    }
}

- (void)fixedTopToSibling:(UIView *)view margin:(CGFloat)margin {
    VHset(self, VT(view) - VT(self) + margin);
}

- (void)fixedTopToParent:(UIView *)view margin:(CGFloat)margin {
    VHset(self, 0 - VT(self) + margin);
}

@end
